﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;


public class EmployeeAddressInfoDAL
    {
        #region 基础方法

        #region 开始生成tomodel提高代码的重用性

        /// 开始生成tomodel提高代码的重用性
        private EmployeeAddressInfoModel ToModel( DataRow row )
        {
            var model = new EmployeeAddressInfoModel();
            model.Id = (Guid) SqlHelper.FromDbValue(row["Id"]);
            model.Provinces = (String) SqlHelper.FromDbValue(row["Provinces"]);
            model.City = (String) SqlHelper.FromDbValue(row["City"]);
            model.County = (String) SqlHelper.FromDbValue(row["County"]);
            model.Address = (String) SqlHelper.FromDbValue(row["Address"]);
            model.Category = (String) SqlHelper.FromDbValue(row["Category"]);
            model.EmployeeId = (Guid) SqlHelper.FromDbValue(row["EmployeeId"]);
            model.AddressAllInfo = (String) SqlHelper.FromDbValue(row["AddressAllInfo"]);
            return model;
        } //tomodel的结束。

        #endregion

        #region listAll开始

        //查询所有EmployeeAddressInfo的数据列表
        public IEnumerable <EmployeeAddressInfoModel> ListAll()
        {
            var list = new List <EmployeeAddressInfoModel>();
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            "Select Id,Provinces,City,County,Address,Category,EmployeeId,AddressAllInfo FROM  EmployeeAddressInfo");
            foreach (DataRow row in table.Rows)
                {
                    EmployeeAddressInfoModel model = ToModel(row);
                    list.Add(model);
                }
            return list;
        } //listAllEmployeeAddressInfo结束

        #endregion

        #region  根据ID来查询EmployeeAddressInfo的信息

        public EmployeeAddressInfoModel GetByEmployeeAddressInfoId( Guid Id )
        {
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            " SELECT Id,Provinces,City,County,Address,Category,EmployeeId,AddressAllInfo FROM  EmployeeAddressInfo  WHERE  Id = @Id",
                            new SqlParameter("@Id", Id));
            if (table.Rows.Count <= 0)
                {
                    return null;
                }
            if (table.Rows.Count == 1)
                {
                    return ToModel(table.Rows[0]);
                }
            throw new Exception();
        }

        #endregion

        #region  根据ID修改EmployeeAddressInfo的信息

        public void UpdateEmployeeAddressInfo( EmployeeAddressInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "  UPDATE  EmployeeAddressInfo SET  Provinces = @Provinces,City = @City,County = @County,Address = @Address,AddressAllInfo = @AddressAllInfo  WHERE  Id = @Id",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@Provinces", model.Provinces),
                            new SqlParameter("@City", model.City),
                            new SqlParameter("@County", model.County),
                            new SqlParameter("@Address", model.Address),
                         
                            new SqlParameter("@AddressAllInfo", model.AddressAllInfo));
        }

        #endregion

        #region 添加户籍所在地EmployeeAddressInfo信息

        // 添加一条户籍所在地EmployeeAddressInfo 的信息
        public void InsertEmployeeAddressInfo( EmployeeAddressInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "INSERT INTO EmployeeAddressInfo  (Id,Provinces,City,County,Address,Category,EmployeeId,AddressAllInfo  ) values (@Id,@Provinces,@City,@County,@Address,@Category,@EmployeeId,@AddressAllInfo)",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@Provinces", model.Provinces),
                            new SqlParameter("@City", model.City),
                            new SqlParameter("@County", model.County),
                            new SqlParameter("@Address", model.Address),
                            new SqlParameter("@Category", model.Category),
                            new SqlParameter("@EmployeeId", model.EmployeeId),
                            new SqlParameter("@AddressAllInfo", model.AddressAllInfo));
        }

        #endregion

        #region 添加现住址EmployeeAddressInfo信息

        // 添加一条现住址EmployeeAddressInfo 的信息
        public void InsertEmployeeNowAddressInfo( EmployeeAddressInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "INSERT INTO EmployeeAddressInfo  (Id,Provinces,City,County,Address,Category,EmployeeId,AddressAllInfo  ) values (@Id,@Provinces,@City,@County,@Address,@Category,@EmployeeId,@AddressAllInfo)",
                            new SqlParameter("@Id", model.Id),
                            new SqlParameter("@Provinces", model.Provinces),
                            new SqlParameter("@City", model.City),
                            new SqlParameter("@County", model.County),
                            new SqlParameter("@Address", model.Address),
                            new SqlParameter("@Category", model.Category),
                            new SqlParameter("@EmployeeId", model.EmployeeId),
                            new SqlParameter("@AddressAllInfo", model.AddressAllInfo));
        }

        #endregion

        //根据ID来查询EmployeeAddressInfo的信息

        //根据ID修改EmployeeAddressInfo的信息

        #endregion
    }

//当前EmployeeAddressInfoDAL类结束